```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入解析Guava EventBus | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f8fafc;
            color: #1e293b;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
        }
        .code-block {
            background-color: #1e293b;
            border-left: 4px solid #7c3aed;
        }
        .highlight-box {
            background: linear-gradient(to right, rgba(124, 58, 237, 0.1), rgba(124, 58, 237, 0.05));
            border-left: 4px solid #7c3aed;
        }
        .section-title {
            position: relative;
            padding-left: 1.5rem;
        }
        .section-title:before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            height: 80%;
            width: 4px;
            background: linear-gradient(to bottom, #7c3aed, #4f46e5);
            border-radius: 2px;
        }
        .hover-scale {
            transition: transform 0.2s ease, box-shadow 0.2s ease;
        }
        .hover-scale:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-4 sm:px-6 lg:px-8">
        <div class="max-w-7xl mx-auto">
            <div class="flex flex-col items-center text-center">
                <div class="inline-flex items-center space-x-2 bg-white/10 px-4 py-2 rounded-full mb-6">
                    <i class="fas fa-code text-purple-300"></i>
                    <span class="text-sm font-medium text-purple-100">Java开发</span>
                </div>
                <h1 class="text-4xl md:text-5xl font-bold mb-6 font-serif">深入解析Guava EventBus</h1>
                <p class="text-xl text-purple-100 max-w-3xl leading-relaxed">
                    当我第一次接触Guava的EventBus时，感觉像发现了一块瑰宝。在复杂系统中，传统的观察者模式写起来太繁琐了，而EventBus让我的代码瞬间清爽了很多。
                </p>
                <div class="mt-10">
                    <div class="inline-flex rounded-md shadow">
                        <a href="#publish-subscribe" class="inline-flex items-center justify-center px-5 py-3 border border-transparent text-base font-medium rounded-md text-purple-700 bg-white hover:bg-purple-50">
                            <i class="fas fa-book-open mr-2"></i> 开始阅读
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <!-- Table of Contents -->
        <div class="bg-white rounded-xl shadow-sm p-6 mb-12 hover-scale">
            <h2 class="text-2xl font-bold mb-6 text-gray-800 flex items-center">
                <i class="fas fa-list-ol text-purple-600 mr-3"></i> 文章目录
            </h2>
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
                <a href="#publish-subscribe" class="flex items-center p-3 hover:bg-purple-50 rounded-lg transition-colors">
                    <div class="flex-shrink-0 h-10 w-10 rounded-full bg-purple-100 flex items-center justify-center">
                        <span class="text-purple-600 font-bold">1</span>
                    </div>
                    <div class="ml-4">
                        <h3 class="text-gray-800 font-medium">发布-订阅模式的实现原理</h3>
                    </div>
                </a>
                <a href="#dispatcher" class="flex items-center p-3 hover:bg-purple-50 rounded-lg transition-colors">
                    <div class="flex-shrink-0 h-10 w-10 rounded-full bg-purple-100 flex items-center justify-center">
                        <span class="text-purple-600 font-bold">2</span>
                    </div>
                    <div class="ml-4">
                        <h3 class="text-gray-800 font-medium">事件分发机制与线程模型</h3>
                    </div>
                </a>
                <a href="#annotations" class="flex items-center p-3 hover:bg-purple-50 rounded-lg transition-colors">
                    <div class="flex-shrink-0 h-10 w-10 rounded-full bg-purple-100 flex items-center justify-center">
                        <span class="text-purple-600 font-bold">3</span>
                    </div>
                    <div class="ml-4">
                        <h3 class="text-gray-800 font-medium">注解处理器与反射应用</h3>
                    </div>
                </a>
                <a href="#architecture" class="flex items-center p-3 hover:bg-purple-50 rounded-lg transition-colors">
                    <div class="flex-shrink-0 h-10 w-10 rounded-full bg-purple-100 flex items-center justify-center">
                        <span class="text-purple-600 font-bold">4</span>
                    </div>
                    <div class="ml-4">
                        <h3 class="text-gray-800 font-medium">构建高效的事件驱动系统</h3>
                    </div>
                </a>
                <a href="#async" class="flex items-center p-3 hover:bg-purple-50 rounded-lg transition-colors">
                    <div class="flex-shrink-0 h-10 w-10 rounded-full bg-purple-100 flex items-center justify-center">
                        <span class="text-purple-600 font-bold">5</span>
                    </div>
                    <div class="ml-4">
                        <h3 class="text-gray-800 font-medium">异步事件处理与错误处理</h3>
                    </div>
                </a>
            </div>
        </div>

        <!-- Content Sections -->
        <div class="space-y-16">
            <!-- Section 1 -->
            <section id="publish-subscribe" class="bg-white rounded-xl shadow-sm overflow-hidden hover-scale">
                <div class="p-6 sm:p-8">
                    <div class="flex items-center mb-6">
                        <div class="flex-shrink-0 h-10 w-10 rounded-full bg-purple-600 flex items-center justify-center">
                            <span class="text-white font-bold">1</span>
                        </div>
                        <h2 class="ml-4 text-2xl font-bold text-gray-800">发布-订阅模式的实现原理</h2>
                    </div>
                    
                    <div class="prose max-w-none">
                        <h3 class="text-xl font-semibold text-gray-800 section-title mb-4">为什么需要事件总线？</h3>
                        <p>在大型应用程序中，组件间通信是一个常见挑战。传统方案各有优缺点：</p>
                        
                        <div class="grid grid-cols-1 md:grid-cols-3 gap-4 my-6">
                            <div class="bg-gray-50 p-4 rounded-lg border border-gray-200">
                                <h4 class="font-medium text-gray-800 mb-2 flex items-center">
                                    <i class="fas fa-link text-purple-500 mr-2"></i> 直接方法调用
                                </h4>
                                <p class="text-gray-600">耦合度高，不灵活</p>
                            </div>
                            <div class="bg-gray-50 p-4 rounded-lg border border-gray-200">
                                <h4 class="font-medium text-gray-800 mb-2 flex items-center">
                                    <i class="fas fa-eye text-purple-500 mr-2"></i> 观察者模式
                                </h4>
                                <p class="text-gray-600">代码量大，类型安全性差</p>
                            </div>
                            <div class="bg-gray-50 p-4 rounded-lg border border-gray-200">
                                <h4 class="font-medium text-gray-800 mb-2 flex items-center">
                                    <i class="fas fa-exchange-alt text-purple-500 mr-2"></i> 回调接口
                                </h4>
                                <p class="text-gray-600">容易陷入回调地狱</p>
                            </div>
                        </div>
                        
                        <div class="highlight-box p-4 my-6 rounded-r">
                            <p class="italic text-gray-700">
                                <i class="fas fa-quote-left text-purple-400 mr-2"></i>
                                我记得有次维护一个遗留系统，里面的组件通信逻辑就像一盘意大利面条 - 错综复杂，牵一发而动全身。添加新功能简直是噩梦，因为你根本搞不清哪些代码会受到影响。
                            </p>
                        </div>
                        
                        <p>Guava的EventBus提供了一种优雅的解决方案，实现了组件间的松散耦合：</p>
                        <ul class="list-disc pl-5 space-y-2 my-4">
                            <li>事件发布者不需要知道谁在监听</li>
                            <li>事件订阅者不需要知道谁发布了事件</li>
                            <li>通过注解简化了API使用</li>
                            <li>支持继承关系的事件类型</li>
                        </ul>
                        
                        <h3 class="text-xl font-semibold text-gray-800 section-title mt-8 mb-4">EventBus的核心概念</h3>
                        <p>EventBus的核心是<strong>发布-订阅模式</strong>（Publish-Subscribe Pattern），这是观察者模式的一种变体。主要包含三个角色：</p>
                        <ol class="list-decimal pl-5 space-y-2 my-4">
                            <li><strong>事件</strong>：包含需要传递的信息</li>
                            <li><strong>发布者</strong>：发布事件到EventBus</li>
                            <li><strong>订阅者</strong>：注册到EventBus并处理感兴趣的事件</li>
                        </ol>
                        
                        <div class="my-8">
                            <div class="mermaid">
                                graph TD
                                    A[发布者] -->|发布事件| B(EventBus)
                                    B -->|分发事件| C[订阅者1]
                                    B -->|分发事件| D[订阅者2]
                                    B -->|分发事件| E[订阅者3]
                            </div>
                        </div>
                        
                        <h4 class="font-semibold text-gray-800 mb-2">示例代码：</h4>
                        <div class="code-block rounded-lg p-4 text-gray-200 font-mono text-sm overflow-x-auto my-4">
                            <pre><code>// 事件类
public class MessageEvent {
    private final String message;
    
    public MessageEvent(String message) {
        this.message = message;
    }
    
    public String getMessage() {
        return message;
    }
}

// 订阅者
public class MessageSubscriber {
    @Subscribe
    public void handleMessage(MessageEvent event) {
        System.out.println("收到消息: " + event.getMessage());
    }
}

// 使用EventBus
EventBus eventBus = new EventBus();
eventBus.register(new MessageSubscriber());
eventBus.post(new MessageEvent("Hello, EventBus!"));</code></pre>
                        </div>
                        <p class="mt-4">这个简单示例就实现了组件间的解耦通信。事件发布者只负责发布事件，不需要关心谁在处理；而订阅者只需要定义好处理方法并注册到EventBus。</p>
                        
                        <h3 class="text-xl font-semibold text-gray-800 section-title mt-8 mb-4">EventBus的核心源码实现</h3>
                        <div class="code-block rounded-lg p-4 text-gray-200 font-mono text-sm overflow-x-auto my-4">
                            <pre><code>// EventBus.java (简化版)
public class EventBus {
    // 存储订阅者信息的注册表
    private final SubscriberRegistry subscribers = new SubscriberRegistry(this);
    
    // 事件分发器
    private final Dispatcher dispatcher;
    
    // 注册订阅者
    public void register(Object object) {
        subscribers.register(object);
    }
    
    // 发布事件
    public void post(Object event) {
        Iterator&lt;Subscriber&gt; eventSubscribers = subscribers.getSubscribers(event);
        if (eventSubscribers.hasNext()) {
            dispatcher.dispatch(event, eventSubscribers);
        }
    }
}</code></pre>
                        </div>
                        <p class="mt-4">核心逻辑其实很简单：</p>
                        <ol class="list-decimal pl-5 space-y-2 my-4">
                            <li>维护一个订阅者注册表</li>
                            <li>当事件发布时，查找对应的订阅者</li>
                            <li>通过分发器将事件传递给订阅者处理</li>
                        </ol>
                        
                        <h3 class="text-xl font-semibold text-gray-800 section-title mt-8 mb-4">订阅者注册表的实现</h3>
                        <div class="code-block rounded-lg p-4 text-gray-200 font-mono text-sm overflow-x-auto my-4">
                            <pre><code>// SubscriberRegistry.java (简化版)
class SubscriberRegistry {
    // 缓存每个事件类型的订阅者列表
    private final ConcurrentMap&lt;Class&lt;?&gt;, CopyOnWriteArraySet&lt;Subscriber&gt;&gt; subscribers = 
            Maps.newConcurrentMap();
    
    // 注册订阅者
    void register(Object object) {
        Map&lt;Class&lt;?&gt;, Collection&lt;Subscriber&gt;&gt; methodsInListener = 
                findAllSubscribers(object);
        
        for (Map.Entry&lt;Class&lt;?&gt;, Collection&lt;Subscriber&gt;&gt; entry : methodsInListener.entrySet()) {
            Class&lt;?&gt; eventType = entry.getKey();
            Collection&lt;Subscriber&gt; eventMethodsInListener = entry.getValue();
            
            CopyOnWriteArraySet&lt;Subscriber&gt; eventSubscribers = subscribers.get(eventType);
            if (eventSubscribers == null) {
                eventSubscribers = new CopyOnWriteArraySet&lt;&gt;();
                subscribers.putIfAbsent(eventType, eventSubscribers);
            }
            
            eventSubscribers.addAll(eventMethodsInListener);
        }
    }
    
    // 查找对象中所有的订阅方法
    private Map&lt;Class&lt;?&gt;, Collection&lt;Subscriber&gt;&gt; findAllSubscribers(Object object) {
        Map&lt;Class&lt;?&gt;, Collection&lt;Subscriber&gt;&gt; methodsInListener = Maps.newHashMap();
        Class&lt;?&gt; clazz = object.getClass();
        
        for (Method method : clazz.getDeclaredMethods()) {
            Subscribe annotation = method.getAnnotation(Subscribe.class);
            if (annotation != null) {
                Class&lt;?&gt;[] parameterTypes = method.getParameterTypes();
                if (parameterTypes.length == 1) {
                    Class&lt;?&gt; eventType = parameterTypes[0];
                    Collection&lt;Subscriber&gt; subscribers = methodsInListener.computeIfAbsent(
                            eventType, k -> new ArrayList&lt;&gt;());
                    subscribers.add(new Subscriber(object, method));
                }
            }
        }
        
        return methodsInListener;
    }
    
    // 获取事件的订阅者
    Iterator&lt;Subscriber&gt; getSubscribers(Object event) {
        Class&lt;?&gt; eventType = event.getClass();
        List&lt;Subscriber&gt; result = new ArrayList&lt;&gt;();
        
        // 查找精确匹配的订阅者
        CopyOnWriteArraySet&lt;Subscriber&gt; eventSubscribers = subscribers.get(eventType);
        if (eventSubscribers != null) {
            result.addAll(eventSubscribers);
        }
        
        // 查找父类和接口的订阅者
        for (Map.Entry&lt;Class&lt;?&gt;, CopyOnWriteArraySet&lt;Subscriber&gt;&gt; entry : subscribers.entrySet()) {
            Class&lt;?&gt; subscribedType = entry.getKey();
            if (subscribedType.isAssignableFrom(eventType) && subscribedType != eventType) {
                result.addAll(entry.getValue());
            }
        }
        
        return result.iterator();
    }
}</code></pre>
                        </div>
                        <p class="mt-4">核心设计点：</p>
                        <ul class="list-disc pl-5 space-y-2 my-4">
                            <li>使用线程安全的ConcurrentMap存储订阅信息</li>
                            <li>使用反射找出标记了@Subscribe注解的方法</li>
                            <li>支持通过继承关系查找匹配的订阅者</li>
                        </ul>
                        <p>这种设计使得EventBus能够处理复杂的事件继承关系，例如订阅了Object类型的方法可以接收任何类型的事件。</p>
                    </div>
                </div>
            </section>

            <!-- Section 2 -->
            <section id="dispatcher" class="bg-white rounded-xl shadow-sm overflow-hidden hover-scale">
                <div class="p-6 sm:p-8">
                    <div class="flex items-center mb-6">
                        <div class="flex-shrink-0 h-10 w-10 rounded-full bg-purple-600 flex items-center justify-center">
                            <span class="text-white font-bold">2</span>
                        </div>
                        <h2 class="ml-4 text-2xl font-bold text-gray-800">事件分发机制与线程模型分析</h2>
                    </div>
                    
                    <div class="prose max-w-none">
                        <h3 class="text-xl font-semibold text-gray-800 section-title mb-4">事件分发器Dispatcher的设计</h3>
                        <div class="code-block rounded-lg p-4 text-gray-200 font-mono text-sm overflow-x-auto my-4">
                            <pre><code>// Dispatcher.java (简化版)
interface Dispatcher {
    void dispatch(Object event, Iterator&lt;Subscriber&gt; subscribers);
}

// 立即分发所有事件
class ImmediateDispatcher implements Dispatcher {
    @Override
    public void dispatch(Object event, Iterator&lt;Subscriber&gt; subscribers) {
        while (subscribers.hasNext()) {
            subscribers.next().dispatchEvent(event);
        }
    }
}

// 排队分发（用于AsyncEventBus）
class AsyncDispatcher implements Dispatcher {
    private final Executor executor;
    
    AsyncDispatcher(Executor executor) {
        this.executor = executor;
    }
    
    @Override
    public void dispatch(Object event, Iterator&lt;Subscriber&gt; subscribers) {
        while (subscribers.hasNext()) {
            final Subscriber subscriber = subscribers.next();
            executor.execute(() -> subscriber.dispatchEvent(event));
        }
    }
}</code></pre>
                        </div>
                        <p class="mt-4">Dispatcher负责确定如何将事件传递给订阅者。EventBus提供了两种主要的调度策略：</p>
                        <ol class="list-decimal pl-5 space-y-2 my-4">
                            <li><strong>ImmediateDispatcher</strong>：在调用post()的线程中同步分发事件</li>
                            <li><strong>AsyncDispatcher</strong>：使用线程池异步分发事件</li>
                        </ol>
                        <p>这种灵活的设计允许根据不同场景选择合适的分发策略。</p>
                        
                        <h3 class="text-xl font-semibold text-gray-800 section-title mt-8 mb-4">线程模型分析</h3>
                        <p>EventBus提供了两种线程模型：</p>
                        
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 my-6">
                            <div class="bg-purple-50 p-5 rounded-lg border border-purple-200">
                                <h4 class="font-semibold text-purple-800 mb-3 flex items-center">
                                    <i class="fas fa-sync-alt mr-2"></i> 同步EventBus
                                </h4>
                                <div class="code-block rounded-lg p-4 text-gray-200 font-mono text-sm overflow-x-auto mb-3">
                                    <pre><code>EventBus eventBus = new EventBus();</code></pre>
                                </div>
                                <p class="text-gray-700">事件在发布线程中同步处理，适合以下场景：</p>
                                <ul class="list-disc pl-5 space-y-1 mt-2 text-gray-700">
                                    <li>事件处理简单且快速</li>
                                    <li>订阅者需要立即响应</li>
                                    <li>需要保证事件按照发布顺序处理</li>
                                </ul>
                            </div>
                            
                            <div class="bg-indigo-50 p-5 rounded-lg border border-indigo-200">
                                <h4 class="font-semibold text-indigo-800 mb-3 flex items-center">
                                    <i class="fas fa-random mr-2"></i> 异步AsyncEventBus
                                </h4>
                                <div class="code-block rounded-lg p-4 text-gray-200 font-mono text-sm overflow-x-auto mb-3">
                                    <pre><code>ExecutorService executor = Executors.newFixedThreadPool(10);
AsyncEventBus asyncEventBus = new AsyncEventBus(executor);</code></pre>
                                </div>
                                <p class="text-gray-700">事件在线程池中异步处理，适合以下场景：</p>
                                <ul class="list-disc pl-5 space-y-1 mt-2 text-gray-700">
                                    <li>耗时的事件处理</li>
                                    <li>需要提高系统的并发性</li>
                                    <li>发布者不需要等待订阅者处理完成</li>
                                </ul>
                            </div>
                        </div>
                        
                        <div class="highlight-box p-4 my-6 rounded-r">
                            <p class="italic text-gray-700">
                                <i class="fas fa-lightbulb text-purple-400 mr-2"></i>
                                我在一个项目中就使用了混合策略：关键业务事件用同步EventBus保证即时处理，而日志、统计等非关键事件用AsyncEventBus提高吞吐量。
                            </p>
                        </div>
                        
                        <h3 class="text-xl font-semibold text-gray-800 section-title mt-8 mb-4">死锁问题和处理机制</h3>
                        <p>使用EventBus时要小心递归事件和死锁问题。例如，如果一个事件处理器在处理A事件时又发布了A事件，就可能导致栈溢出。</p>
                        <p>Guava提供了DeadEvent类来处理未被消费的事件：</p>
                        
                        <div class="code-block rounded-lg p-4 text-gray-200 font-mono text-sm overflow-x-auto my-4">
                            <pre><code>public class DeadEventLogger {
    @Subscribe
    public void handleDeadEvent(DeadEvent deadEvent) {
        System.err.println("无人处理的事件: " + deadEvent.getEvent());
    }
}

// 注册DeadEvent处理器
eventBus.register(new DeadEventLogger());</code></pre>
                        </div>
                        <p class="mt-4">当发布一个没有订阅者的事件时，EventBus会将其包装为DeadEvent并重新发布，这样我们就可以捕获并记录这些"死亡"事件。</p>
                    </div>
                </div>
            </section>

            <!-- Section 3 -->
            <section id="annotations" class="bg-white rounded-xl shadow-sm overflow-hidden hover-scale">
                <div class="p-6 sm:p-8">
                    <div class="flex items-center mb-6">
                        <div class="flex-shrink-0 h-10 w-10 rounded-full bg-purple-600 flex items-center justify-center">
                            <span class="text-white font-bold">3</span>
                        </div>
                        <h2 class="ml-4 text-2xl font-bold text-gray-800">注解处理器实现与反射应用</h2>
                    </div>
                    
                    <div class="prose max-w-none">
                        <h3 class="text-xl font-semibold text-gray-800 section-title mb-4">@Subscribe注解的设计与实现</h3>
                        <div class="code-block rounded-lg p-4 text-gray-200 font-mono text-sm overflow-x-auto my-4">
                            <pre><code>@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Beta
public @interface Subscribe {
    // 处理方法的优先级
    int priority() default 0;
}</code></pre>
                        </div>
                        <p class="mt-4">这个注解使用Java反射机制在运行时识别订阅方法。EventBus通过反射扫描类中所有带有@Subscribe注解的方法，并将它们注册为事件处理器。</p>
                        
                        <p>注解处理的核心逻辑：</p>
                        <div class="code-block rounded-lg p-4 text-gray-200 font-mono text-sm overflow-x-auto my-4">
                            <pre><code>// SubscriberRegistry.java (简化版)
private Map&lt;Class&lt;?&gt;, Collection&lt;Subscriber&gt;&gt; findAllSubscribers(Object object) {
    Map&lt;Class&lt;?&gt;, Collection&lt;Subscriber&gt;&gt; methodsInListener = Maps.newHashMap();
    Class&lt;?&gt; clazz = object.getClass();
    
    // 查找当前类的所有方法
    for (Method method : clazz.getDeclaredMethods()) {
        // 检查是否有@Subscribe注解
        Subscribe annotation = method.getAnnotation(Subscribe.class);
        if (annotation != null) {
            // 检查方法参数
            Class&lt;?&gt;[] parameterTypes = method.getParameterTypes();
            if (parameterTypes.length == 1) {
                // 事件类型是方法的唯一参数
                Class&lt;?&gt; eventType = parameterTypes[0];
                // 创建Subscriber对象
                Subscriber subscriber = new Subscriber(object, method);
                
                Collection&lt;Subscriber&gt; subscribers = methodsInListener.computeIfAbsent(
                        eventType, k -> new ArrayList&lt;&gt;());
                subscribers.add(subscriber);
            }
        }
    }
    
    return methodsInListener;
}</code></pre>
                        </div>
                        <p class="mt-4">这种基于注解的方式大大简化了API使用，使代码更加简洁优雅。</p>
                        
                        <h3 class="text-xl font-semibold text-gray-800 section-title mt-8 mb-4">Subscriber对象的设计</h3>
                        <div class="code-block rounded-lg p-4 text-gray-200 font-mono text-sm overflow-x-auto my-4">
                            <pre><code>// Subscriber.java (简化版)
class Subscriber {
    // 订阅者对象
    private final Object target;
    // 处理方法
    private final Method method;
    
    Subscriber(Object target, Method method) {
        this.target = target;
        this.method = method;
        method.setAccessible(true); // 允许调用非公共方法
    }
    
    // 分发事件到订阅者
    void dispatchEvent(Object event) {
        try {
            method.invoke(target, event);
        } catch (IllegalAccessException | InvocationTargetException e) {
            // 处理异常
        }
    }
}</code></pre>
                        </div>
                        <p class="mt-4">核心设计点：</p>
                        <ul class="list-disc pl-5 space-y-2 my-4">
                            <li>缓存目标对象和方法引用以提高性能</li>
                            <li>使用setAccessible(true)允许调用私有方法</li>
                            <li>使用反射调用方法处理事件</li>
                        </ul>
                        <p>这种设计使得EventBus能够调用任何方法，而不受访问修饰符的限制。</p>
                        
                        <h3 class="text-xl font-semibold text-gray-800 section-title mt-8 mb-4">反射性能优化</h3>
                        <p>反射调用通常比直接方法调用慢，但Guava做了一些优化：</p>
                        <ol class="list-decimal pl-5 space-y-2 my-4">
                            <li><strong>缓存反射结果</strong>：EventBus缓存了类的方法信息，避免重复扫描</li>
                            <li><strong>setAccessible优化</strong>：预先设置方法为可访问，减少反射调用开销</li>
                            <li><strong>避免包装和拆箱</strong>：直接使用对象引用而非原始类型，减少自动装箱开销</li>
                        </ol>
                        
                        <div class="highlight-box p-4 my-6 rounded-r">
                            <p class="italic text-gray-700">
                                <i class="fas fa-exclamation-triangle text-purple-400 mr-2"></i>
                                实际上，我在一个高性能交易系统中发现EventBus的反射调用确实成为了性能瓶颈。我们最终采用了编译时代码生成的方式，为热点路径创建直接调用的代码，绕过了反射机制。
                            </p>
                        </div>
                    </div>
                </div>
            </section>

            <!-- Section 4 -->
            <section id="architecture" class="bg-white rounded-xl shadow-sm overflow-hidden hover-scale">
                <div class="p-6 sm:p-8">
                    <div class="flex items-center mb-6">
                        <div class="flex-shrink-0 h-10 w-10 rounded-full bg-purple-600 flex items-center justify-center">
                            <span class="text-white font-bold">4</span>
                        </div>
                        <h2 class="ml-4 text-2xl font-bold text-gray-800">构建高效的事件驱动系统</h2>
                    </div>
                    
                    <div class="prose max-w-none">
                        <p>让我分享一个真实案例。在一个电商系统中，我们需要在用户下单后触发多个操作：</p>
                        <ol class="list-decimal pl-5 space-y-2 my-4">
                            <li>发送确认邮件</li>
                            <li>更新库存</li>
                            <li>计算积分</li>
                            <li>推送消息给商家</li>
                            <li>记录统计数据</li>
                        </ol>
                        
                        <p>传统实现可能导致强耦合或代码膨胀。使用EventBus，我们构建了一个优雅的解决方案：</p>
                        
                        <div class="code-block rounded-lg p-4 text-gray-200 font-mono text-sm overflow-x-auto my-4">
                            <pre><code>// 订单事件
public class OrderCreatedEvent {
    private final Order order;
    
    public OrderCreatedEvent(Order order) {
        this.order = order;
    }
    
    public Order getOrder() {
        return order;
    }
}

// 邮件服务
public class EmailService {
    @Subscribe
    public void sendConfirmationEmail(OrderCreatedEvent event) {
        Order order = event.getOrder();
        // 发送确认邮件...
        System.out.println("发送邮件到: " + order.getCustomerEmail());
    }
}

// 库存服务
public class InventoryService {
    @Subscribe
    public void updateInventory(OrderCreatedEvent event) {
        Order order = event.getOrder();
        // 更新库存...
        System.out.println("更新商品库存: " + order.getItems());
    }
}

// 积分服务
public class LoyaltyService {
    @Subscribe
    public void calculatePoints(OrderCreatedEvent event) {
        // 计算积分...
        System.out.println("为订单添加积分: " + event.getOrder().getId());
    }
}

// 使用EventBus
public class OrderService {
    private final EventBus eventBus;
    
    public OrderService() {
        this.eventBus = new EventBus();
        // 注册所有处理器
        eventBus.register(new EmailService());
        eventBus.register(new InventoryService());
        eventBus.register(new LoyaltyService());
    }
    
    public void createOrder(Order order) {
        // 保存订单...
        System.out.println("创建订单: " + order.getId());
        
        // 发布事件
        eventBus.post(new OrderCreatedEvent(order));
    }
}</code></pre>
                        </div>
                        
                        <p class="mt-4">这个设计有几个关键优势：</p>
                        <ul class="list-disc pl-5 space-y-2 my-4">
                            <li><strong>解耦</strong>：OrderService不需要知道谁在处理订单创建事件</li>
                            <li><strong>可扩展</strong>：添加新的处理器不需要修改OrderService</li>
                            <li><strong>可测试</strong>：每个组件可以单独测试</li>
                            <li><strong>灵活</strong>：可以根据需要动态注册或注销处理器</li>
                        </ul>
                        
                        <p>但坦白说，这样的设计也有挑战：</p>
                        <ul class="list-disc pl-5 space-y-2 my-4">
                            <li><strong>隐式依赖</strong>：代码阅读者可能难以理解事件流</li>
                            <li><strong>调试困难</strong>：错误可能发生在事件处理器中而非发布点</li>
                            <li><strong>顺序控制</strong>：处理器执行顺序不容易控制</li>
                        </ul>
                        
                        <div class="highlight-box p-4 my-6 rounded-r">
                            <p class="italic text-gray-700">
                                <i class="fas fa-book-open text-purple-400 mr-2"></i>
                                我们通过清晰的文档和约定缓解了这些问题。例如，我们规定所有事件类必须以"Event"结尾，并采用统一的注册方式。
                            </p>
                        </div>
                    </div>
                </div>
            </section>

            <!-- Section 5 -->
            <section id="async" class="bg-white rounded-xl shadow-sm overflow-hidden hover-scale">
                <div class="p-6 sm:p-8">
                    <div class="flex items-center mb-6">
                        <div class="flex-shrink-0 h-10 w-10 rounded-full bg-purple-600 flex items-center justify-center">
                            <span class="text-white font-bold">5</span>
                        </div>
                        <h2 class="ml-4 text-2xl font-bold text-gray-800">异步事件处理与错误处理</h2>
                    </div>
                    
                    <div class="prose max-w-none">
                        <h3 class="text-xl font-semibold text-gray-800 section-title mb-4">AsyncEventBus的实现</h3>
                        <p>AsyncEventBus是EventBus的异步版本，允许在线程池中处理事件：</p>
                        
                        <div class="code-block rounded-lg p-4 text-gray-200 font-mono text-sm overflow-x-auto my-4">
                            <pre><code>// AsyncEventBus.java (简化版)
public class AsyncEventBus extends EventBus {
    public AsyncEventBus(Executor executor) {
        super("default", executor, Dispatcher.immediate(), LoggingHandler.INSTANCE);
    }
}</code></pre>
                        </div>
                        
                        <p>使用非常简单：</p>
                        <div class="code-block rounded-lg p-4 text-gray-200 font-mono text-sm overflow-x-auto my-4">
                            <pre><code>// 创建线程池
ExecutorService executor = Executors.newFixedThreadPool(10);

// 创建异步事件总线
AsyncEventBus asyncEventBus = new AsyncEventBus(executor);

// 注册订阅者
asyncEventBus.register(new EmailService());

// 发布事件
asyncEventBus.post(new OrderCreatedEvent(order));</code></pre>
                        </div>
                        
                        <p>AsyncEventBus的关键是使用AsyncDispatcher，它在线程池中执行订阅者的方法：</p>
                        <div class="code-block rounded-lg p-4 text-gray-200 font-mono text-sm overflow-x-auto my-4">
                            <pre><code>// AsyncDispatcher (简化)
class AsyncDispatcher implements Dispatcher {
    private final Executor executor;
    
    @Override
    public void dispatch(Object event, Iterator&lt;Subscriber&gt; subscribers) {
        while (subscribers.hasNext()) {
            final Subscriber subscriber = subscribers.next();
            executor.execute(() -> subscriber.dispatchEvent(event));
        }
    }
}</code></pre>
                        </div>
                        
                        <h3 class="text-xl font-semibold text-gray-800 section-title mt-8 mb-4">错误处理策略</h3>
                        <p>EventBus提供了一种处理订阅者抛出异常的机制 - SubscriberExceptionHandler：</p>
                        <div class="code-block rounded-lg p-4 text-gray-200 font-mono text-sm overflow-x-auto my-4">
                            <pre><code>// SubscriberExceptionHandler.java
public interface SubscriberExceptionHandler {
    void handleException(Throwable exception, SubscriberExceptionContext context);
}</code></pre>
                        </div>
                        
                        <p>默认实现是将异常记录到日志，但你可以提供自定义实现：</p>
                        <div class="code-block rounded-lg p-4 text-gray-200 font-mono text-sm overflow-x-auto my-4">
                            <pre><code>// 自定义异常处理器
SubscriberExceptionHandler handler = (exception, context) -> {
    System.err.println("处理事件时发生异常: " + context.getEvent());
    exception.printStackTrace();
    
    // 可以记录到监控系统、发送警报等
    alertService.sendAlert("EventBus异常: " + exception.getMessage());
};

// 使用自定义处理器创建EventBus
EventBus eventBus = new EventBus(handler);</code></pre>
                        </div>
                        
                        <div class="highlight-box p-4 my-6 rounded-r">
                            <p class="italic text-gray-700">
                                <i class="fas fa-bug text-purple-400 mr-2"></i>
                                在生产环境中，一个好的错误处理策略至关重要，因为事件处理通常发生在与发布者不同的上下文中，使得错误可能被忽略。
                            </p>
                            <p class="italic text-gray-700 mt-2">
                                我曾经在一个项目中遇到过这样的问题：异步事件处理器抛出的异常被默默吞掉，导致系统看起来正常运行，但某些操作实际上没有完成。添加自定义异常处理器后，我们能够捕获这些错误并通过监控系统及时发现问题。
                            </p>
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8 px-4 sm:px-6 lg:px-8">
        <div class="max-w-7xl mx-auto">
            <div class="flex flex-col items-center">
                <div class="text-xl font-bold text-white mb-2">技术小馆</div>
                <a href="http://www.yuque.com/jtostring" class="text-purple-300 hover:text-white transition-colors duration-200">
                    <i class="fas fa-globe mr-1"></i> http://www.yuque.com/jtostring
                </a>
                <div class="mt-4 text-sm text-gray-500">
                    &copy; 2023 技术小馆. 保留所有权利.
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'dark',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```